import {requestHeader} from "./serverManager";
import Taro from '@tarojs/taro'

export class HttpProvider {
  /**
   * 发送GET请求
   * @param cmd:string
   * @param data:object
   * @return RequestTask<any>
   */
  async getData(cmd, data = null) {
    const params = {
      url: cmd,
      data: data,
      method: "GET",
      header: requestHeader.getHeader(),
    };
    return await Taro.request(params).then(res => res.data);
  }

  /**
   * 发送POST请求
   * @param cmd
   * @param data
   * @return RequestTask<any>
   */
  async postData(cmd, data = null) {
    const params = {
      url: cmd,
      data: data,
      method: "POST",
      header: requestHeader.getHeader(),
    };
    return await Taro.request(params).then(res => res.data)
  }

  /**
   * 发送PUT请求
   * @param cmd
   * @param data
   * @return RequestTask<any>
   */
  async putData(cmd, data = null) {
    const params = {
      url: cmd,
      data: data,
      method: "PUT",
      header: requestHeader.getHeader(),
    };
    return await Taro.request(params).then(res => res.data)
  }

  /**
   * 发送DELETE请求
   * @param cmd
   * @param data
   * @return RequestTask<any>
   */
  async deleteData(cmd, data = null) {
    const params = {
      url: cmd,
      data: data,
      method: "DELETE",
      header: requestHeader.getHeader(),
    };
    return Taro.request(params);
  }

  buildParamsGET(data) {
    let cmd = '?';
    for (let key in data) {
      cmd = cmd + key + '=' + data[key]
    }
    return cmd
  }

  buildParamsDELETE(data){

  }

}
